package cn.jiyun.provider.service.impl;

import cn.jiyun.provider.common.R;
import cn.jiyun.provider.dao.DeptDao;
import cn.jiyun.provider.dao.EmpDao;
import cn.jiyun.provider.pojo.Dept;
import cn.jiyun.provider.pojo.Emp;
import cn.jiyun.provider.pojo.User;
import cn.jiyun.provider.pojo.dto.EmpDto;
import cn.jiyun.provider.service.EmpService;
import cn.jiyun.provider.utils.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletResponse;
import java.util.LinkedHashMap;
import java.util.List;

@Service
@Slf4j
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpDao empDao;
    @Autowired
    private DeptDao deptDao;

    @Override
    public R findPage(Integer page, Integer pageSize,String name) {
        Page<Emp> empPage = new Page<>(page,pageSize);
        /*Page<Emp> empPage1 = empDao.selectPage(empPage, null);*/
        Page<Emp> empPage1 = empDao.selectEmpPage(empPage,name);
        return R.success(empPage1);
    }

    @Override
    public void add(Emp emp) {
        empDao.insert(emp);
    }

    @Override
    public Emp findById(Integer id) {
        return empDao.selectById(id);
    }

    @Override
    public void edit(Emp emp) {
        empDao.updateById(emp);
    }

    @Override
    public void delete(Integer id) {
        empDao.deleteById(id);
    }

    @Override
    public void exportExcel(HttpServletResponse response) {
        String[] tou={"姓名","用户名","年龄","创建时间","部门名称"};
        String[] ziduan={"name","userName","age","createTime","did"};
        List<Emp> users = empDao.selectList(null);
        if(CollectionUtils.isEmpty(users)){
            log.info("集合为空:{}",users.toArray());
        }
        LinkedHashMap<String, String> map = new LinkedHashMap<>();

        for (int i = 0; i < ziduan.length; i++) {
            map.put(ziduan[i],"");
        }
        ExcelUtil.exportExcel(users.size(),response,users,tou,map);
    }

    @Override
    public List<Dept> getDept() {
        return deptDao.selectList(null);
    }


    @Override
    public List<Emp> getEmpAll() {
        return empDao.selectList(null);
    }
}
